﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WZ.WebSpider
{
    public class CompanyMarketDataSpider:IWebSpider<Entity.CompanyMarketData>
    {
        public CompanyMarketDataSpider(string code)
        {
            this.Code = code;
        }
        public string Code
        {
            get;
            set;
        }

        public string Url
        {
            get { return string.Format("http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd={0}&sty=GBFDB&st=z&sr=&p=&ps=&cb=jQuery183030554042996029085_1483080593935&js=&token=5c46f660fab8722944521b8807de07c0&_=1483080610322", this.Code); }
        }

        public Encoding ContextEncoding
        {
            get { return Encoding.UTF8; }
        }

        public System.Text.RegularExpressions.Regex regex
        {
            get { return new Regex("<ul class=\"gbsiderhxdata\" id=\"gbsiderhxdata\"><li><a href=\".+\">收益</a>（三）：(?<Earnings>.+)</li><li>PE（动）：(?<PE>.+)</li><li><a href=\".+\">净资产</a>：(?<NetAsset>.+)</li><li>市净率：(?<PB>.+)</li><li>收入：(?<Income>.+)亿</li><li><a href=\".+\">同比</a>：(?<Income_yoy>.+)%</li><li>净利润：(?<NetIncome>.+)亿</li><li>同比：(?<NetIncome_yoy>.+)%</li><li><a href=\".+\">毛利率</a>：(?<GrossMargin>.+)%</li><li>净利率：(?<NetMargin>.+)%</li><li><a href=\"。+\">ROE</a>：(?<ROE>.+)%</li><li>负债率：(?<DebtRatio>.+)%</li><li>总股本：(?<TSI>.+)亿</li><li>总值：(?<MktCap>.+)亿</li><li>流通股：(?<CirculationStock>.+)亿</li><li>流值：(?<CirculationStockValue>.+)亿</li><li class=\"hline\">每股未分配利润：(?<UDPPS>.+)元</li><li class=\"hline\">上市时间：(.+)</li></ul>"); }
        }

        public List<Entity.CompanyMarketData> GetWebData(string urlParam)
        {
            //WZ.Utils.WebHttp.HttpClient client = new Utils.WebHttp.HttpClient(this.ContextEncoding);
            System.Net.HttpWebRequest request = HttpWebRequest.CreateHttp(this.Url);
            request.Referer = "http://guba.eastmoney.com/list,600318.html";
            //string webStr = client.GetString(this.Url);
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393";
            request.Accept = "application/javascript, */*; q=0.8";
            string webStr = WZ.Utils.WebHttp.HttpClient.StringFromResponse((HttpWebResponse)request.GetResponse(), this.ContextEncoding);
            MatchCollection matchs = regex.Matches(webStr);
            List<Entity.CompanyMarketData> entity = new List<Entity.CompanyMarketData>();
            for (int i = 0; i < matchs.Count; i++)
            {

                entity.Add(WZ.WebSpider.Serialize.ConverModel.FillModel<Entity.CompanyMarketData>(matchs[i].Groups));
            }
            return entity;
        }

        public bool Save(Entity.CompanyMarketData t)
        {
            throw new NotImplementedException();
        }

        public int DepIndex
        {
            get { throw new NotImplementedException(); }
        }
    }
}
